import { IAuth } from '@/interface';
import { $get, $post, $put, $delete } from '@/request/api';

/** 权限列表(分页) */
export function fetchAuthList(params) {
  return $get('/auth/list', params);
}

/** 权限列表(不分页) */
export function fetchAllList() {
  return $get('/auth/all_list');
}

/** 获取树型权限 */
export function fetchTreeAuth(id?: number) {
  return $get(`/auth/get_tree_auth`, { id });
}

/** 获取除了父级以外的所有权限（树型） */
export function fetchTreeChildAuth() {
  return $get(`/auth/get_tree_child_auth`);
}

/** 获取该权限的子权限（只找一层） */
export function fetchGetChildAuth(id: number) {
  return $get(`/auth/get_child_auth/${id}`);
}

/** 给某个权限批量设置子权限 */
export function fetchSetAddChildAuth({ id, c_auths }: IAuth) {
  return $put(`/auth/set_add_child_auth`, { id, c_auths });
}

/** 获取该权限的子权限（递归查找所有） */
export function fetchAllChildAuth(id: number) {
  return $get(`/auth/get_all_child_auth/${id}`);
}

/** 获取某个权限的权限 */
export function fetchAuthAuth(id) {
  return $get(`/auth/get_auth_auth/${id}`);
}

/** 新增权限 */
export function fetchCreateAuth({
  p_id,
  auth_name,
  auth_value,
  type,
  priority,
}: IAuth) {
  return $post(`/auth/create`, {
    p_id,
    auth_name,
    auth_value,
    type,
    priority,
  });
}

/** 修改权限 */
export function fetchUpdateAuth({
  id,
  p_id,
  auth_name,
  auth_value,
  type,
  priority,
}: IAuth) {
  return $put(`/auth/update/${id}`, {
    p_id,
    auth_name,
    auth_value,
    type,
    priority,
  });
}

/** 批量删除子权限 */
export function fetchBatchDeleteChildAuths({ id, c_auths }: IAuth) {
  return $delete(`/auth/batch_delete_child_auths`, {
    id,
    c_auths,
  });
}

/** 批量新增子权限 */
export function fetchBatchAddChildAuths({ id, c_auths }: IAuth) {
  return $put(`/auth/batch_add_child_auths`, {
    id,
    c_auths,
  });
}

/** 删除权限 */
export function fetchDeleteAuth(id: number) {
  return $delete(`/auth/delete/${id}`);
}
